package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BMaterialQuantitiesEntity;
import com.hongxun.modules.api.service.BMaterialQuantitiesService;
import com.hongxun.modules.bss.entity.APBaseEntity;
import com.hongxun.modules.bss.entity.DMaterialCostEntity;
import com.hongxun.modules.bss.service.APBaseService;
import com.hongxun.modules.bss.service.DMaterialCostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.DMaterialBuyEntity;
import com.hongxun.modules.bss.service.DMaterialBuyService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;


/**
 * 点位材料购置表
 *
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-04 11:17:40
 */
@RestController
@RequestMapping("/bss/dmaterialbuy")
public class DMaterialBuyController {
    @Autowired
    private DMaterialBuyService dMaterialBuyService;
    @Autowired
    private DMaterialCostService dMaterialCostService;
    @Autowired
    private APBaseService aPBaseService;
    @Autowired
    private BMaterialQuantitiesService bMaterialQuantitiesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";

        if (query.get("pGuid") != null && !query.get("pGuid").toString().equals("")) {
            wheres += " and  p_guid = '" + query.get("pGuid").toString() + "' ";
        }
        if (query.get("dpGuid") != null && !query.get("dpGuid").toString().equals("")) {
            wheres += " and  p_p_guid = '" + query.get("dpGuid").toString() + "' ";
        }
        if (query.get("qName") != null && !query.get("qName").toString().equals("")) {
            wheres += " and  m_b_name like  '%" + query.get("qName").toString() + "%' ";
        }
        if (query.get("qLabel") != null && !query.get("qLabel").toString().equals("")) {
            wheres += " and  d_label like '%" + query.get("qLabel").toString() + "%' ";
        }
        wheres += " and  m_b_price>0 ";
        wheres += " and  m_b_datastate != '删除' ";

        query.put("filterSql", wheres + " ");
        List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryList(query);
        int total = dMaterialBuyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(dMaterialBuyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    @RequestMapping("/appList")
    public R appList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        String wheres = " 1=1 ";

        if (query.get("pGuid") != null && !query.get("pGuid").toString().equals("")) {
            wheres += " and  p_guid = '" + query.get("pGuid").toString() + "' ";
        }
        if (query.get("qName") != null && !query.get("qName").toString().equals("")) {
            wheres += " and  m_b_name like  '%" + query.get("qName").toString() + "%' ";
        }
        if (query.get("qLabel") != null && !query.get("qLabel").toString().equals("")) {
            wheres += " and  d_label like '%" + query.get("qLabel").toString() + "%' ";
        }

        wheres += " and  m_b_price>0 ";
        wheres += " and  m_b_datastate != '删除' ";

        query.put("filterSql", wheres + " ");
        List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryAppList(query);
        int total = dMaterialBuyService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(dMaterialBuyList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{guid}")
    public R info(@PathVariable("guid") String guid) {
        DMaterialBuyEntity dMaterialBuy = dMaterialBuyService.queryObject(guid);

        return R.ok().put("dMaterialBuy", dMaterialBuy);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody DMaterialBuyEntity dMaterialBuy) {

		//	这段是大修技改项目要求，大荔和遵义项目打包时，请注释该段代码
		//	新增之前先查询点位设备表中是否有和需要添加数据中的设备id相同的数据，如有则将新增该条数据的单价改为表中已有同设备id数据的单价，再计算合价
		/*Map<String , Object> objectMap = new HashMap<>();
		String whereMaterId = " 1=1 and m_m_guid='"+dMaterialBuy.getMMGuid()+"' ";
		objectMap.put("filterSql",whereMaterId);
		List<DMaterialBuyEntity> dMaterialBuyEntities = dMaterialBuyService.queryList(objectMap);
		if(dMaterialBuyEntities != null){
			int ios = 0;
			for (DMaterialBuyEntity dMaterialBuyEntity : dMaterialBuyEntities) {
				if(ios == 0)
					dMaterialBuy.setMBPrice(dMaterialBuyEntity.getMBPrice());	//	单价
				++ios;
			}
			dMaterialBuy.setMBPriceall(dMaterialBuy.getMBCount().multiply(dMaterialBuy.getMBPrice()));	//	合价
			dMaterialBuyService.save(dMaterialBuy);
		}else{
			dMaterialBuy.setMBPriceall(Priceall(dMaterialBuy.getMBPrice(), dMaterialBuy.getMBCount()));
			dMaterialBuyService.save(dMaterialBuy);
		}*/


		//	以下是遵义和大荔项目，打包时需要放开注释的两行代码
    	dMaterialBuy.setMBPriceall(Priceall(dMaterialBuy.getMBPrice(), dMaterialBuy.getMBCount()));
        dMaterialBuyService.save(dMaterialBuy);
        APBaseEntity aPBaseEntity = aPBaseService.queryObject(dMaterialBuy.getPGuid());
        Map<String, Object> mapaPOther = new HashMap<>();
        String wheres = " 1=1 ";
        wheres += " and  m_guid  = '" + dMaterialBuy.getMMGuid() + "' ";
        wheres += " and  m_q_type =  " + aPBaseEntity.getPBType();
        wheres += " and  m_q_work = 1 ";
        mapaPOther.put("filterSql", wheres + " ");
        List<BMaterialQuantitiesEntity> listBMaterialQuantities = bMaterialQuantitiesService.queryList(mapaPOther);
        for (BMaterialQuantitiesEntity BMaterialQuantitiesEntity : listBMaterialQuantities) {
            DMaterialCostEntity dMaterialCostEntity = new DMaterialCostEntity();
            dMaterialCostEntity.setPPGuid(dMaterialBuy.getPPGuid());
            dMaterialCostEntity.setPGuid(dMaterialBuy.getPGuid());
            dMaterialCostEntity.setPMGuid(dMaterialBuy.getGuid());
            dMaterialCostEntity.setMMGuid(dMaterialBuy.getMMGuid());
            dMaterialCostEntity.setMCWork("1");
            dMaterialCostEntity.setMCQuantities(BMaterialQuantitiesEntity.getMQQuantities());
            dMaterialCostEntity.setMCExplain(BMaterialQuantitiesEntity.getMQExplain());
            dMaterialCostEntity.setMCUnit(BMaterialQuantitiesEntity.getMQUnit());
            dMaterialCostEntity.setMCMeterUnit(BMaterialQuantitiesEntity.getMQMeterUnit());
            dMaterialCostEntity.setMCCount(dMaterialBuy.getMBCount());
            dMaterialCostEntity.setMCPrice(BMaterialQuantitiesEntity.getMQPrice());
            dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCCount(), dMaterialCostEntity.getMCPrice()));
            dMaterialCostEntity.setMCRatio(BMaterialQuantitiesEntity.getMQRatio());
            dMaterialCostEntity.setMCDatastate("新增");
            dMaterialCostEntity.setMMName(dMaterialBuy.getMBName());
            dMaterialCostEntity.setMMType(1);
            dMaterialCostEntity.setPMSource(4);
            dMaterialCostService.save(dMaterialCostEntity);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody DMaterialBuyEntity dMaterialBuy) {

        dMaterialBuy.setMBPriceall(Priceall(dMaterialBuy.getMBPrice(), dMaterialBuy.getMBCount()));
        dMaterialBuyService.update(dMaterialBuy);
        Map<String, Object> mapaPOther = new HashMap<>();
        String wheres = " 1=1 ";
        wheres += " and  p_m_guid = '" + dMaterialBuy.getGuid() + "' ";
        mapaPOther.put("filterSql", wheres + " ");
        List<DMaterialCostEntity> dMaterialCostList = dMaterialCostService.queryList(mapaPOther);
        for (DMaterialCostEntity dMaterialCostEntity : dMaterialCostList) {
            dMaterialCostEntity.setMCCount(dMaterialBuy.getMBCount());
            dMaterialCostEntity.setMCPriceall(Priceall(dMaterialCostEntity.getMCPrice(), dMaterialCostEntity.getMCCount()));
            dMaterialCostService.update(dMaterialCostEntity);
        }
        return R.ok();
    }

    /**
     * 修改单价和数量
     *
     * @param Parms
     * @return
     */
    @RequestMapping("/updateCell")
    public R update(@RequestParam Map<String, Object> Parms) {

        DMaterialBuyEntity dMaterialBuy = dMaterialBuyService.queryObject(Parms.get("guid").toString());
        BigDecimal Price = new BigDecimal(Parms.get("mBPrice").toString());
        dMaterialBuy.setMBPrice(Price);
        dMaterialBuy.setMBPriceall(Priceall(Price, dMaterialBuy.getMBCount()));
        dMaterialBuy.setMBDatastate("修改");
        dMaterialBuyService.update(dMaterialBuy);


        return R.ok();
    }

    @RequestMapping("/upPriceother")
    public R upPriceother(@RequestBody String guid) {
        DMaterialBuyEntity dMaterialBuy = dMaterialBuyService.queryObject(guid);

        Map<String, Object> mapaPOther = new HashMap<>();
        String wheres = " 1=1 ";
        wheres += " and  p_guid = '" + dMaterialBuy.getPGuid() + "' ";
        wheres += " and  m_m_guid = '" + dMaterialBuy.getMMGuid() + "' ";
        mapaPOther.put("filterSql", wheres + " ");
        List<DMaterialBuyEntity> dMaterialBuyList = dMaterialBuyService.queryList(mapaPOther);
        for (DMaterialBuyEntity dMaterialBuyEntity : dMaterialBuyList) {
            dMaterialBuyEntity.setMBPrice(dMaterialBuy.getMBPrice());

            dMaterialBuyEntity.setMBPriceall(Priceall(dMaterialBuy.getMBPrice(), dMaterialBuyEntity.getMBCount()));
            dMaterialBuyEntity.setMBDatastate("修改");
            dMaterialBuyService.update(dMaterialBuyEntity);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] guids) {
        dMaterialBuyService.deleteBatch(guids);
        dMaterialCostService.deleteBatchByMBguid(guids);

        return R.ok();
    }


    /**
     *  大修技改手机端修改价格
     * @param Parms
     * @return
     */
    @RequestMapping("/updateCellForPhone")
    public R updateCellForPhone(@RequestParam Map<String, Object> Parms) {

        DMaterialBuyEntity dMaterialBuy = dMaterialBuyService.queryObject(Parms.get("guid").toString());
        BigDecimal Price = new BigDecimal(Parms.get("mBPrice").toString());
        dMaterialBuy.setMBPrice(Price);
        dMaterialBuy.setMBPriceall(Priceall(Price, dMaterialBuy.getMBCount()));
        dMaterialBuy.setMBDatastate("修改");
        dMaterialBuyService.update(dMaterialBuy);
        upPriceother(Parms.get("guid").toString());

        return R.ok();
    }





    private static BigDecimal Priceall(BigDecimal Price, BigDecimal Count) {

        BigDecimal Priceall = null;
        Priceall = Price.multiply(Count);
        return Priceall;
    }

}
